define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data('area',["60%","94%"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'news/news/index',
                    add_url: 'news/news/add',
                    edit_url: 'news/news/edit',
                    del_url: 'news/news/del',
                    multi_url: 'news/news/multi',
                    table: 'web_news',
                    dragsort_url: '',
                },
                showExport: false
            });
            //批量发布
            $(document).on("click", ".btn-issue", function () {
                var data = table.bootstrapTable('getSelections');
                var ids = [];
                if (data.length === 0) {
                    Toastr.error("请选择操作信息");
                    return;
                }

                for (var i = 0; i < data.length; i++) {
                    ids[i] = data[i]['id']
                }

                Layer.confirm(
                    '确认选中的' + ids.length + '条批量发布吗?', {
                        icon: 3,
                        title: __('Warning'),
                        offset: '40%',
                        shadeClose: true
                    },
                    function (index) {
                        Layer.close(index);
                        Backend.api.ajax({
                            url: "news/news/checkids",
                            data: {
                                ids: JSON.stringify(ids),
                                is_release : 1
                            }
                        }, function (data, ret) { //成功的回调
                            if (ret.code === 1) {
                                table.bootstrapTable('refresh');
                                Layer.close(index);
                            } else {
                                Layer.close(index);
                                Toastr.error(ret.msg);
                            }
                        }, function (data, ret) { //失败的回调
                            // Toastr.error(ret.msg);
                            Layer.close(index);
                        });
                    }
                );

            });
            //批量下架
            $(document).on("click", ".btn-del", function () {
                var data = table.bootstrapTable('getSelections');
                var ids = [];
                if (data.length === 0) {
                    Toastr.error("请选择操作信息");
                    return;
                }
                for (var i = 0; i < data.length; i++) {
                    ids[i] = data[i]['id']
                }

                Layer.confirm(
                    '确认选中的' + ids.length + '条批量下架吗?', {
                        icon: 3,
                        title: __('Warning'),
                        offset: '40%',
                        shadeClose: true
                    },
                    function (index) {
                        Layer.close(index);
                        Backend.api.ajax({
                            url: "news/news/checkids",
                            data: {
                                ids: JSON.stringify(ids),
                                is_release : 0
                            }
                        }, function (data, ret) { //成功的回调
                            if (ret.code === 1) {

                                table.bootstrapTable('refresh');
                                Layer.close(index);
                            } else {
                                Layer.close(index);
                                Toastr.error(ret.msg);
                            }
                        }, function (data, ret) { //失败的回调
                            // Toastr.error(ret.msg);
                            Layer.close(index);
                        });
                    }
                );
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh ASC, id desc',
                columns: [
                    [
                        { field: 'state', checkbox: true, },
                        { field: 'id', title: __('Id') ,operate: false},
                        //{ field: 'type', title: __('Type'),  formatter: Table.api.formatter.label, searchList: { 1: __('Type editor'), 2: __('Type hang') }, sortable: true },
                        { field: 'category.name', title: __('Type'), operate: 'LIKE' ,operate: false},
                        { field: 'title', title: __('Title'), operate: 'LIKE'},
                        { field: 'author', title: __('Author'), operate: 'LIKE'},
                        { field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        // { field: 'content', title: __('Content'), operate: false },
                        { field: 'weigh', title: __('Weigh'), sortable: true, operate: false },
                        {
                            field: 'is_release',
                            title: __('Is release'),
                            align: 'center',
                            formatter: Table.api.formatter.toggle,
                            searchList: { 0: __('Is release false'), 1: __('Is release true') }
                        },
                        { field: 'create_time', title: __('Create time'), addclass: 'datetimerange', datetimeFormat: 'YYYY-MM-DD' ,operate: false},
                        { field: 'update_time', title: __('Update time'), addclass: 'datetimerange', datetimeFormat: 'YYYY-MM-DD' ,operate: false},
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["60%","94%"]);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            this.api.bindevent();
        },
        edit: function () {
            this.api.bindevent();
        },

        api: {
            data: {
                defaultWeigh: 1
            },
            bindevent: function () {
                var self = this;

                Form.api.bindevent($("form[role=form]"));
                var old = $('#c-content').val();
                $('.btn-reset').click(function () {
                    $('#c-content').summernote('code',old);
                });

                //self.changeType($('input[name="row[type]"]:checked').val());
                //$('input[name="row[type]"]').change(function () {
                //    var type = $(this).val();
                //    self.changeType(type);
                //});
            },
            //changeType: function (type) {
            //    if (type == 1) {
            //        $(".type-url").hide();
            //        $(".type-editor").show();
            //    } else {
            //        $(".type-editor").hide();
            //        $(".type-url").show();
            //    }
            //},
        }
    };
    return Controller;
});